<?php
apf_require_controller("QaAbstract");
class Qa_Finance_QaListController extends QaAbstractController{

    public function handle_request(){
        $params = $this->request->get_parameters();
        $left_ids = array_keys(Const_Qa::get_left_list_id(Const_Qa::TAG_FINANCE));
        if(!isset($params['cate']) || !is_numeric($params['cate']) || !in_array($params['cate'], $left_ids)){
            $params['cate'] = Const_Qa::TAG_FINANCE_BIAODAN;
        }
        $params['search_text'] = htmlspecialchars(trim($params['search_text']));
        if(!$params['search_text']){
            $this->request->set_attribute('no_search_content', true);
        }
        if(!$params['p'] || $params['p'] < 1){
            $this->page_num = 1;
        }else{
            $this->page_num = $params['p'];
        }

        //获取搜索结果
        $search_result = $this->get_question_bll()->get_question_search(Const_Qa::TAG_FINANCE, $params['search_text']);
        //获取组装数据
        $data = $this->_build_data($search_result, $params['cate']);
        $params['cate'] = $data['cate_now'];
        if(empty($data['current_list'])){
            $this->request->set_attribute('no_search_content', true);
        }
        //匹配字段
        $data['current_list'] = $this->_search_match_text($params['search_text'],$data['current_list']);
        //显示分页数据
        $list = array();
        if(!empty($data['current_list'])){
            $list = array_slice($data['current_list'], ($this->page_num - 1) * $this->page_rows, $this->page_rows);
        }

        //页面参数
        $this->request->set_attribute('cate', $params['cate']);
        $this->request->set_attribute('left_list', Const_Qa::get_left_list_id(Const_Qa::TAG_FINANCE));
        $this->request->set_attribute('search_text', $params['search_text']);
        $this->request->set_attribute('search_num', $data['search_num']);
        $this->request->set_attribute('list', $list);
        $this->request->set_attribute('multipage', $this->_get_page($data['current_list'], $this->page_num, $params));
        $this->request->set_attribute('page_num', $this->page_num);
        $this->request->set_attribute('page_rows', $this->page_rows);
        $this->request->set_attribute('permission', $_SESSION[Const_Qa::get_menu_list_en(Const_Qa::TAG_FINANCE)]);
        return "Qa_Finance_QaList";
    }
    /**
     * 组装数据
     * @param unknown_type $list
     */
    private function _build_data($list,$cate){
        //搜索结果数据
        $search_num = array ();
        //
        $cate_now = 10000;
        //当前显示的数据
        $current_list = array ();
        foreach($list as $key => $value){
            //判断是不是当前显示的问答
            $tmp_cate = $value['cate'];
            $current_list[$tmp_cate][] = $value;
            if($cate_now > $tmp_cate){
                $cate_now = $tmp_cate;
            }
            $search_num[$tmp_cate] += 1;
        }
        if(isset($current_list[$cate])){
            $cate_now = $cate;
        }
        //总条数
        $search_num['all'] = count($list);
        //计算总页数
        $page_total = ceil($search_num['all'] / $this->page_rows);
        if($this->page_num < 1){
            $this->page_num = 1;
        }elseif($this->page_num > $page_total){
            $this->page_num = $page_total;
        }
        return array ("search_num" => $search_num, "current_list" => $current_list[$cate_now] ,"cate_now"=>$cate_now);
    }
    private function _search_match_text($search_text,$list){
        $replace = "<span class='searchMatch'>{$search_text}</span>";
        if(is_array($list)){
            foreach($list as $key => $value){
                $len = strlen($search_text);
                $sta = strpos($value['title'],$search_text);
                $list[$key]['title'] = substr($value['title'], 0,$sta).$replace.substr($value['title'], $sta+$len);
            }
        }
        return $list;
    }
    /**
     * 获取分页
     * Enter description here ...
     */
    private function _get_page($total, $curr_page, $param){
        $_where['p'] = $curr_page;
        $_where['search_text'] = $param['search_text'];
        $_where['cate'] = $param['cate'];
        $url_suffix = "";
        $url_prefix = "";
        if(count($total) > $this->page_rows){
            $result = Util_MultiPage::multiPage($url_prefix, $_where, $url_suffix, count($total), $this->page_rows, 10, false, true);
        }
        return $result;
    }

}